<template>
  <section class="app-inner">
    <el-dialog v-if="dialogVisible" :visible.sync="dialogVisible" :before-close="handleClose" :title="title"
      width="800px" modal>
      <div class="header">
        <el-form size="small" label-width="84px" :inline="true" :model="queryParams">
          <el-form-item>
            <el-button type="text" @click="addTeacherFn">+ 添加</el-button>
            <el-button type="text" @click="refreshTeacherFn" icon="el-icon-refresh">刷新</el-button>
          </el-form-item>
          <el-form-item class="searchCss">
            <el-input clearable placeholder="搜索人员姓名" v-model="queryParams.name" @input="searchTeacher"
              @keyup.native.enter="search"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <div class="footerTable">
        <el-table ref="multipleTable" :data="teacherTableData" tooltip-effect="dark" style="width: 100%">
          <el-table-column type="index" align="center" width="25">
            <template slot-scope="scope">
              <el-radio v-model="scope.row.isSelect" :label="scope.row.no" @change="changeRedio($event, scope.row)">
                &nbsp;</el-radio>
            </template>
          </el-table-column>
          <el-table-column prop="name" label="姓名"></el-table-column>
          <el-table-column prop="number" label="职工号"></el-table-column>
        </el-table>
        <el-pagination class="paginationCss" v-if="teacherTableData.length > 0" layout="total, prev, pager, next"
          @current-change="changePage" :current-page="queryParams.pageNum" :page-size="queryParams.pageSize"
          :total="totalCount"></el-pagination>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleCancel" size="small">取 消</el-button>
        <el-button type="primary" @click="hanldeSave" size="small">确 定</el-button>
      </div>
      <div style="margin-top: 20px"></div>
    </el-dialog>
    <addTeachInfo :infoData="infoData" :deptData="departments" :addTeachForm="addTeachForm" :data="addTeachFormData"
      @closeaddTeachForm="addTeachForm.flag = false" />
  </section>
</template>

<script>
import { queryPage } from '../js/teacherInfo.js'
import addTeachInfo from './addTeachInfo'
import { queryDeptListBySchoolNo } from '@/views/children/basic/js/teacherInfo'
import { decrypt } from '@/utils/crypto'
export default {
  name: 'addTeacher',
  components: {
    addTeachInfo
  },
  provide () {
    return {
      handleSearch: this.search
    }
  },
  data () {
    return {
      dialogVisible: false,
      title: '',
      currentRowId: null,
      totalCount: 0,
      teachForm: {
        flag: false,
        title: ''
      },
      infoData: {},
      type: '',
      queryParams: {
        positionType: '1',
        pageNum: 1,
        pageSize: 10
      },
      bindRowKey (row) {
        return row.no
      },
      detail: {},
      departments: [],
      // 新增
      addTeachForm: {
        flag: false,
        title: ''
      },
      addTeachFormData: {},
      teacherName: '',
      teacherTableData: [],
      selectRow: {}
    }
  },
  computed: {},
  methods: {
    changeRedio (event, row) {
      this.teacherTableData.forEach(item => {
        if (item.isSelect) {
          delete item.isSelect
        }
      })
      this.currentRowId = event
      this.selectRow = row
    },
    async add ({ rowD, type }) {
      this.type = type
      await this.getTeacherList(this.queryParams)
      this.detail = rowD
      this.dialogVisible = true
      this.queryDeptListBySchoolNo()
    },
    search () {
      this.queryParams.pageNum = 1
      this.getTeacherList(this.queryParams)
    },
    searchTeacher (val) {
      this.queryParams.pageNum = 1
      this.getTeacherList(this.queryParams)
    },
    // 获取教师信息 只获取岗位为教师的老师列表
    async getTeacherList (data) {
      try {
        let res = await queryPage(data)
        res.data = JSON.parse(decrypt(res.data))
        if (res.code === 0) {
          this.teacherTableData = res.data.records
          this.totalCount = res.data.total
        }
      } catch (e) { }
    },
    // 关闭弹窗
    handleClose () {
      this.handleCancel()
      // Object.assign(this.$data, this.$options.data())
      // this.dialogVisible = false
    },
    // 保存修改
    hanldeSave () {
      if (this.type === 'editor') {
        this.$emit('addTeacherReq', { data: this.selectRow, type: '' })
      } else if (this.type === 'addHeaderName') {
        this.$emit('addTeacherReq', { data: this.selectRow, type: 'headName' })
      } else {
        this.$emit('selectTeacherFn', this.selectRow)
      }
      Object.assign(this.$data, this.$options.data())
      this.dialogVisible = false
    },
    // 取消修改
    handleCancel () {
      Object.assign(this.$data, this.$options.data())
      this.dialogVisible = false
    },
    // 更改页数
    changePage (val) {
      this.queryParams.pageNum = val
      this.getTeacherList(this.queryParams)
    },
    // 新增教师，跳转到新增教职工
    addTeacherFn () {
      this.addTeachForm.flag = true
      this.addTeachForm.title = '新增教职工信息'
      this.infoData = this.$dictionary
      this.addTeachFormData = {
        isCreateAccount: 1,
        gender: 0,
        cardType: '1',
        nationality: '156',
        positionType: '0'
      }
    },
    // 部门数据
    queryDeptListBySchoolNo () {
      queryDeptListBySchoolNo({}).then(res => {
        this.departments = res.data
      })
    },
    // 刷新教师列表
    refreshTeacherFn () {
      this.queryParams.name = ''
      this.queryParams.pageNum = 1
      let query = {
        positionType: '1',
        pageNum: 1,
        pageSize: 10
      }
      this.getTeacherList(query)
    },
    handleSelectionChange (val) {
      this.multipleSelection = val
    }
  },
  mounted () { }
}
</script>
<style scoped lang="scss">
.searchCss {
  float: right;
}
</style>
